/*
 *  Copyright (C) 2006 Takeharu KATO
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <errno.h>
#include <glib.h>
#include "common.h"

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define IPMSG_INTERNAL_CODE "UTF-8"
#define IPMSG_PROTO_CODE    IPMSG_EXTERNAL_CHARCODE

GStaticMutex convert_mutex = G_STATIC_MUTEX_INIT;
int
convert_string_internal(const char *string,const gchar **to_string){
  int rc=0;
  gchar *converted_string=NULL;
  gsize read_len;
  gsize write_len;
  GError *error_info=NULL;

  if ( (!string) || (!to_string) )
    return -EINVAL;

  g_static_mutex_lock(&convert_mutex);
  converted_string=g_convert((const gchar *)string,
			     -1, /* ヌルターミネート */
			     IPMSG_INTERNAL_CODE,
			     IPMSG_PROTO_CODE,
			     &read_len,
			     &write_len,
			     &error_info);
  rc=-EINVAL;
  if (!converted_string) {
    if (error_info) {
      err_out("%s\n",error_info->message);
      rc=error_info->code;
      g_error_free(error_info);
    }
    if (rc>0)
      rc=-rc;
    g_static_mutex_unlock(&convert_mutex);
    return rc;
  }

  *to_string=converted_string;
  g_static_mutex_unlock(&convert_mutex);
  return 0;
}

int
convert_string_ipmsg_proto(const char *string,const gchar **to_string){
  int rc=0;
  gchar *converted_string;
  gsize read_len;
  gsize write_len;
  GError *error_info=NULL;

  if ( (!string) || (!to_string) )
    return -EINVAL;

  g_static_mutex_lock(&convert_mutex);
  converted_string=g_convert((const gchar *)string,
			     -1, /* ヌルターミネート */
			     IPMSG_PROTO_CODE,
			     IPMSG_INTERNAL_CODE,
			     &read_len,
			     &write_len,
			     &error_info);
  rc=-EINVAL;
  if (!converted_string) {
    if (error_info) {
      err_out("%s\n",error_info->message);
      rc=error_info->code;
      g_error_free(error_info);
    }
    if (rc>0)
      rc=-rc;
    g_static_mutex_unlock(&convert_mutex);
    return rc;
  }

  *to_string=converted_string;
  g_static_mutex_unlock(&convert_mutex);

  return 0;
}

int
convert_string_ipmsg_locale(const char *string,const gchar **to_string){
  int rc=0;
  gchar *converted_string;
  gsize read_len;
  gsize write_len;
  GError *error_info=NULL;

  if ( (!string) || (!to_string) )
    return -EINVAL;

  g_static_mutex_lock(&convert_mutex);
  converted_string=g_locale_from_utf8((const gchar *)string,
			     -1, /* ヌルターミネート */
			     &read_len,
			     &write_len,
			     &error_info);
  if (!converted_string) {
    if (error_info) {
      err_out("%s\n",error_info->message);
      rc=error_info->code;
      g_error_free(error_info);
    }
    if (rc>0)
      rc=-rc;
    g_static_mutex_unlock(&convert_mutex);
    return rc;
  }
  *to_string=converted_string;
  g_static_mutex_unlock(&convert_mutex);
  return 0;
}

int
convert_string_ipmsg_filename(const char *string,const gchar **to_string){
  int rc=0;
  gchar *converted_string;
  gsize read_len;
  gsize write_len;
  GError *error_info=NULL;

  if ( (!string) || (!to_string) )
    return -EINVAL;

  g_static_mutex_lock(&convert_mutex);
  converted_string=g_filename_from_utf8((const gchar *)string,
			     -1, /* ヌルターミネート */
			     &read_len,
			     &write_len,
			     &error_info);

  rc=-EINVAL;
  if (!converted_string) {
    if (error_info) {
      err_out("%s\n",error_info->message);
      rc=error_info->code;
      g_error_free(error_info);
    }
    if (rc>0)
      rc=-rc;
    g_static_mutex_unlock(&convert_mutex);
    return rc;
  }

  *to_string=converted_string;
  g_static_mutex_unlock(&convert_mutex);
  return 0;
}
